{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# IRIS Flowers Classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-13T20:59:31.544688Z",
     "iopub.status.busy": "2024-02-13T20:59:31.544224Z",
     "iopub.status.idle": "2024-02-13T20:59:32.975501Z",
     "shell.execute_reply": "2024-02-13T20:59:32.975050Z"
    }
   },
   "outputs": [],
   "source": [
    "from functools import partial\n",
    "\n",
    "from mltraq import Run, create_session\n",
    "from sklearn.cluster import KMeans\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.dummy import DummyClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.utils import shuffle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-13T20:59:32.978094Z",
     "iopub.status.busy": "2024-02-13T20:59:32.977810Z",
     "iopub.status.idle": "2024-02-13T20:59:32.980439Z",
     "shell.execute_reply": "2024-02-13T20:59:32.980080Z"
    }
   },
   "outputs": [],
   "source": [
    "def load(run: Run):\n",
    "    # Load the IRIS dataset, taking care of shuffling the samples.\n",
    "    # We use run.vars, accessible only within the execution of the runs.\n",
    "    run.vars.X, run.vars.y = shuffle(*load_iris(return_X_y=True), random_state=run.params.seed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-13T20:59:32.982267Z",
     "iopub.status.busy": "2024-02-13T20:59:32.982125Z",
     "iopub.status.idle": "2024-02-13T20:59:32.984879Z",
     "shell.execute_reply": "2024-02-13T20:59:32.984594Z"
    }
   },
   "outputs": [],
   "source": [
    "def train_predict(run: Run):\n",
    "    # Instantiate and train classifier on 100 samples (50 random samples left for evaluation).\n",
    "    model = run.params.classifier(random_state=run.params.seed).fit(run.vars.X[:100], run.vars.y[:100])\n",
    "\n",
    "    # Track the classifier name on run.fields, persisted to database.\n",
    "    run.fields.model_name = model.__class__.__name__\n",
    "\n",
    "    # Use trained model to make predictions.\n",
    "    run.vars.y_pred = model.predict(run.vars.X[100:])\n",
    "    run.vars.y_true = run.vars.y[100:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-13T20:59:32.986639Z",
     "iopub.status.busy": "2024-02-13T20:59:32.986508Z",
     "iopub.status.idle": "2024-02-13T20:59:32.988579Z",
     "shell.execute_reply": "2024-02-13T20:59:32.988263Z"
    }
   },
   "outputs": [],
   "source": [
    "def evaluate(run: Run):\n",
    "    # Track accuracy score from previously determined predictions.\n",
    "    run.fields.accuracy = accuracy_score(run.vars.y_true, run.vars.y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-13T20:59:32.990258Z",
     "iopub.status.busy": "2024-02-13T20:59:32.990122Z",
     "iopub.status.idle": "2024-02-13T20:59:32.998669Z",
     "shell.execute_reply": "2024-02-13T20:59:32.998341Z"
    }
   },
   "outputs": [],
   "source": [
    "# Connect to the MLtraq session and craete an experiment.\n",
    "session = create_session()\n",
    "experiment = session.create_experiment()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-13T20:59:33.000467Z",
     "iopub.status.busy": "2024-02-13T20:59:33.000347Z",
     "iopub.status.idle": "2024-02-13T20:59:33.005718Z",
     "shell.execute_reply": "2024-02-13T20:59:33.005393Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "Experiment(name=\"wp7t47\", runs.count=50, id=\"b3d9b2d8-d2a2-4d0f-82e5-e887a3194ad1\")"
      ],
      "text/plain": [
       "<mltraq.experiment.Experiment at 0x11f886660>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Use a parameter grid to define the experiment's runs.\n",
    "experiment.add_runs(\n",
    "    classifier=[\n",
    "        partial(DummyClassifier, strategy=\"most_frequent\"),\n",
    "        partial(LogisticRegression, max_iter=1000),\n",
    "        partial(KMeans, n_clusters=3, n_init=\"auto\"),\n",
    "        DecisionTreeClassifier,\n",
    "        RandomForestClassifier,\n",
    "    ],\n",
    "    seed=range(10),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-13T20:59:33.035136Z",
     "iopub.status.busy": "2024-02-13T20:59:33.034977Z",
     "iopub.status.idle": "2024-02-13T20:59:35.863037Z",
     "shell.execute_reply": "2024-02-13T20:59:35.862203Z"
    }
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "79f337ed982446e78cf48159870f6e22",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  2%|2         | 1/50 [00:02<01:39,  2.03s/it]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Experiment(name=\"wp7t47\", runs.count=50, id=\"b3d9b2d8-d2a2-4d0f-82e5-e887a3194ad1\")"
      ],
      "text/plain": [
       "<mltraq.experiment.Experiment at 0x11f886660>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Execute experiment, running in parallel the step functions on each run.\n",
    "experiment.execute(steps=[load, train_predict, evaluate])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-13T20:59:35.866174Z",
     "iopub.status.busy": "2024-02-13T20:59:35.865898Z",
     "iopub.status.idle": "2024-02-13T20:59:35.882640Z",
     "shell.execute_reply": "2024-02-13T20:59:35.882142Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>accuracy</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>model_name</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>LogisticRegression</th>\n",
       "      <td>0.960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RandomForestClassifier</th>\n",
       "      <td>0.952</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DecisionTreeClassifier</th>\n",
       "      <td>0.938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>KMeans</th>\n",
       "      <td>0.336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DummyClassifier</th>\n",
       "      <td>0.288</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        accuracy\n",
       "model_name                      \n",
       "LogisticRegression         0.960\n",
       "RandomForestClassifier     0.952\n",
       "DecisionTreeClassifier     0.938\n",
       "KMeans                     0.336\n",
       "DummyClassifier            0.288"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(experiment.runs.df().groupby(\"model_name\").mean(numeric_only=True).sort_values(by=\"accuracy\", ascending=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "0d4d2d33e8844bf7b3ecc2a129beadb3": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "2.0.0",
       "_model_name": "HTMLModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "2.0.0",
       "_view_name": "HTMLView",
       "description": "",
       "description_allow_html": false,
       "layout": "IPY_MODEL_919c8a93cc054ec2b27b87d358f233d2",
       "placeholder": "​",
       "style": "IPY_MODEL_662b9aa6927c4d1bbf8e4387315239dc",
       "tabbable": null,
       "tooltip": null,
       "value": " 50/50 [00:02&lt;00:00, 17.81it/s]"
      }
     },
     "184d7f0b46aa44a68a71f99b7a26be99": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "2.0.0",
       "_model_name": "ProgressStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "2.0.0",
       "_view_name": "StyleView",
       "bar_color": null,
       "description_width": ""
      }
     },
     "46ed48b3731240918d3ee19bbd78082b": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "2.0.0",
       "_model_name": "HTMLModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "2.0.0",
       "_view_name": "HTMLView",
       "description": "",
       "description_allow_html": false,
       "layout": "IPY_MODEL_c932b8e67bf44987b04527dd89a5d9b2",
       "placeholder": "​",
       "style": "IPY_MODEL_724ca659512a4eeaa31b65253e7b9523",
       "tabbable": null,
       "tooltip": null,
       "value": "100%"
      }
     },
     "5b1ede432e1b4495be7e2def78b851bd": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "2.0.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "2.0.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border_bottom": null,
       "border_left": null,
       "border_right": null,
       "border_top": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": "hidden",
       "width": null
      }
     },
     "662b9aa6927c4d1bbf8e4387315239dc": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "2.0.0",
       "_model_name": "HTMLStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "2.0.0",
       "_view_name": "StyleView",
       "background": null,
       "description_width": "",
       "font_size": null,
       "text_color": null
      }
     },
     "724ca659512a4eeaa31b65253e7b9523": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HTMLStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "2.0.0",
       "_model_name": "HTMLStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "2.0.0",
       "_view_name": "StyleView",
       "background": null,
       "description_width": "",
       "font_size": null,
       "text_color": null
      }
     },
     "79f337ed982446e78cf48159870f6e22": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "HBoxModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "2.0.0",
       "_model_name": "HBoxModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "2.0.0",
       "_view_name": "HBoxView",
       "box_style": "",
       "children": [
        "IPY_MODEL_46ed48b3731240918d3ee19bbd78082b",
        "IPY_MODEL_ee164ede639f4cb78bf2d72e481b67ae",
        "IPY_MODEL_0d4d2d33e8844bf7b3ecc2a129beadb3"
       ],
       "layout": "IPY_MODEL_5b1ede432e1b4495be7e2def78b851bd",
       "tabbable": null,
       "tooltip": null
      }
     },
     "919c8a93cc054ec2b27b87d358f233d2": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "2.0.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "2.0.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border_bottom": null,
       "border_left": null,
       "border_right": null,
       "border_top": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "c932b8e67bf44987b04527dd89a5d9b2": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "2.0.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "2.0.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border_bottom": null,
       "border_left": null,
       "border_right": null,
       "border_top": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "ee164ede639f4cb78bf2d72e481b67ae": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "2.0.0",
      "model_name": "FloatProgressModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "2.0.0",
       "_model_name": "FloatProgressModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "2.0.0",
       "_view_name": "ProgressView",
       "bar_style": "",
       "description": "",
       "description_allow_html": false,
       "layout": "IPY_MODEL_f3834e53e9f6458382b65da49a87cd13",
       "max": 50.0,
       "min": 0.0,
       "orientation": "horizontal",
       "style": "IPY_MODEL_184d7f0b46aa44a68a71f99b7a26be99",
       "tabbable": null,
       "tooltip": null,
       "value": 50.0
      }
     },
     "f3834e53e9f6458382b65da49a87cd13": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "2.0.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "2.0.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "2.0.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border_bottom": null,
       "border_left": null,
       "border_right": null,
       "border_top": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
